package com.baizhi.controller;

import com.baizhi.service.BannerService;
import com.baizhi.entity.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @RequestMapping("getAll")
    @ResponseBody
    public Map getAllBanners(){
        HashMap map = new HashMap();
        try {
            map.put("data", bannerService.findAll());
            map.put("code", 0);
            return map;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @ResponseBody
    @RequestMapping("getOne")
    public Banner getBannerById(Banner banner){
        try {
            return bannerService.findBannerByField(banner);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("addOne")
    @ResponseBody
    public Boolean addBanner(Banner banner){
        try {
            bannerService.addBanner(banner);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @ResponseBody
    @RequestMapping("delById")
    public Boolean deleteBannerById(Banner banner){
        try {
            bannerService.removeBannerById(banner.getBannerId());
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @ResponseBody
    @RequestMapping("delBulk")
    public Boolean deleteMultiple(Integer[] ids) {
        try {
            bannerService.removeMultiple(ids);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public Boolean updateBanner(Banner banner) {
        try {
            bannerService.renovateBanner(banner);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
